package com.sgcc.ln.pmos.e_ln_home.activity;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ScreenUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.arouter.home.HomeArouterPath;
import com.sgcc.ln.pmos.common.base.BasePresenter;
import com.sgcc.ln.pmos.common.base.MvpBaseLandActivity;
import com.sgcc.ln.pmos.common.bean.ChartDataBean;
import com.sgcc.ln.pmos.common.widget.CHorizontalScrollView;
import com.sgcc.ln.pmos.common.widget.CurveLineChartView;
import com.sgcc.ln.pmos.e_ln_home.adapter.CurveEditorRVSAdapter;
import com.sgcc.ln.pmos.e_ln_home.adapter.CurveEditorlineRVAdapter;
import com.sgcc.ln.pmos.e_ln_home.bean.DDSNDLPHYDBean;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import me.jessyan.autosize.utils.AutoSizeUtils;

/**
 * 横批曲线
 */
@Route(path = HomeArouterPath.HOME_CURVE)
public class CurveActivity extends MvpBaseLandActivity {

    private RelativeLayout mRlLeft1;
    private RecyclerView mRvLine;
    private CHorizontalScrollView mChsvContent;
    private CurveLineChartView mLcvContent1;
    private RecyclerView mHlvContentG;
    private RelativeLayout mRlClose;
    private TextView mTvTotal;
    private RelativeLayout mTopLayout;
    private RelativeLayout mRlParent;
    private View mEmptyView;
    private TextView mTv5;
    private TextView mTv4;
    private TextView mTv3;
    private TextView mTv2;
    private TextView mTv1;
    private RecyclerView.OnScrollListener[] scrollListeners;
    public int max, min;
    private int mStepSpace = 0;
    private int YMax = 0;
    private List<String> xData;
    private List<ChartDataBean> datas1;
    private CurveEditorRVSAdapter mTextAdapter;
    private CurveEditorlineRVAdapter mLineAdapter;
    private List<DDSNDLPHYDBean.DataBean.IpConfigDataColsListBean> mList;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_curve;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initViews() {
        super.initViews();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        findViewByIds();
        String json = getIntent().getStringExtra("json");
        Gson gson = new Gson();
        Type type = new TypeToken<List<DDSNDLPHYDBean.DataBean.IpConfigDataColsListBean>>() {
        }.getType();
        mList = gson.fromJson(json, type);
        setViewData();
        mRlClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void findViewByIds() {
        mRlLeft1 = (RelativeLayout) findViewById(R.id.rl_left1);
        mRvLine = (RecyclerView) findViewById(R.id.rv_line);
        mChsvContent = (CHorizontalScrollView) findViewById(R.id.chsv_content);
        mLcvContent1 = (CurveLineChartView) findViewById(R.id.lcv_content_1);
        mHlvContentG = (RecyclerView) findViewById(R.id.hlv_content_g);
        mRlClose = (RelativeLayout) findViewById(R.id.rl_close);
        mTvTotal = (TextView) findViewById(R.id.tv_total);
        mTopLayout = (RelativeLayout) findViewById(R.id.top_layout);
        mEmptyView = (View) findViewById(R.id.empty_view);
        mRlParent = findViewById(R.id.rl_parent);
        mTv5 = findViewById(R.id.tv_5);
        mTv4 = findViewById(R.id.tv_4);
        mTv3 = findViewById(R.id.tv_3);
        mTv2 = findViewById(R.id.tv_2);
        mTv1 = findViewById(R.id.tv_1);
    }

    private void setYShaft(int dataMax) {
        int v = (int) (dataMax * 1.2);
        if (v > 100000) {
            max = (int) ((v / 100000 + 1) * 100000);
        } else if (v > 10000) {
            max = (int) ((v / 10000 + 1) * 10000);
        } else if (v > 1000) {
            max = (int) ((v / 1000 + 1) * 1000);
        } else if (v > 100) {
            max = (int) ((v / 100 + 1) * 100);
        } else {
            max = 100;
        }
        min = 0;
        int each = max / 5;
        YMax = each * 5;
        mTv5.setText(each * 5 + "");
        mTv4.setText(each * 4 + "");
        mTv3.setText(each * 3 + "");
        mTv2.setText(each * 2 + "");
        mTv1.setText(each + "");
    }

    private void setViewMargins() {
        if (xData.size() > 8) {
            mStepSpace = AutoSizeUtils.dp2px(mContext, 100);
        } else {
            mStepSpace = (ScreenUtils.getScreenWidth()) / (xData.size());
            if (mStepSpace / 2 > AutoSizeUtils.dp2px(mContext, 100)) {
                int margin_width = mStepSpace / 2 - AutoSizeUtils.dp2px(mContext, 100);
                mStepSpace = (ScreenUtils.getScreenWidth() + margin_width) / (xData.size());
            } else {
                int margin_width = AutoSizeUtils.dp2px(mContext, 100) - mStepSpace / 2;
                mStepSpace = (ScreenUtils.getScreenWidth() - margin_width) / (xData.size());
            }
        }
        mRvLine.setPadding(0, AutoSizeUtils.dp2px(mContext, 85), 0, AutoSizeUtils.dp2px(mContext, 90));

        RelativeLayout.LayoutParams linearParams1 = (RelativeLayout.LayoutParams) mRlParent.getLayoutParams();
        linearParams1.setMargins(AutoSizeUtils.dp2px(mContext, 50), AutoSizeUtils.dp2px(mContext, 85), 0, AutoSizeUtils.dp2px(mContext, 90));
        linearParams1.width = AutoSizeUtils.dp2px(mContext, 100);
        mRlParent.setLayoutParams(linearParams1);

        RelativeLayout.LayoutParams linearParams2 = (RelativeLayout.LayoutParams) mEmptyView.getLayoutParams();
        linearParams2.setMargins(0, AutoSizeUtils.dp2px(mContext, 85), 0, AutoSizeUtils.dp2px(mContext, 60));
        linearParams2.width = AutoSizeUtils.dp2px(mContext, 100);
        mEmptyView.setLayoutParams(linearParams2);

    }

    private int getMaxValue() {
        List<Double> resultList = new ArrayList<>();
        for (DDSNDLPHYDBean.DataBean.IpConfigDataColsListBean bean : mList) {
            resultList.add(new BigDecimal(bean.getColName()).stripTrailingZeros().doubleValue());
        }
        return Collections.max(resultList).intValue();
    }

    @SuppressLint("NewApi")
    private void setViewData() {
        xData = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            xData.add(mList.get(i).getColTitle());
        }
        int dataMax = getMaxValue();
        setYShaft(dataMax);

        setViewMargins();

        mHlvContentG.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        mRvLine.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));

        mTextAdapter = new CurveEditorRVSAdapter(mContext, mStepSpace);
        mLineAdapter = new CurveEditorlineRVAdapter(mContext, mStepSpace, max);

        datas1 = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            ChartDataBean chartDataBean = new ChartDataBean();
            chartDataBean.setXkey(mList.get(i).getColTitle());
            chartDataBean.setValue(new BigDecimal(mList.get(i).getColName()).stripTrailingZeros().intValue());
            chartDataBean.setPriceValue(new BigDecimal(mList.get(i).getColName()).stripTrailingZeros().toPlainString());
            datas1.add(chartDataBean);
        }
        mLcvContent1.setVisibility(View.VISIBLE);
        mLcvContent1.setXData(xData);
        mLcvContent1.setMaxMin(max, min);
        mLcvContent1.setMaxMin(max, min);
        mLcvContent1.setStepSpace(mStepSpace);
        mLcvContent1.setPaintType(1);
        mLcvContent1.setData(datas1);
        mLcvContent1.setLineColor(mContext.getResources().getColor(R.color._09B1FF));


        mHlvContentG.setAdapter(mTextAdapter);
        mRvLine.setAdapter(mLineAdapter);

        mTextAdapter.addDatas(datas1);
        mLineAdapter.addDatas(datas1);

        setViewScrollListener();

    }

    private void setViewScrollListener() {
        scrollListeners = new RecyclerView.OnScrollListener[4];
        scrollListeners[0] = new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                totalDx += dx;
                mTopLayout.setVisibility(totalDx == 0 ? View.GONE : View.VISIBLE);
                mHlvContentG.removeOnScrollListener(scrollListeners[1]);

                mHlvContentG.scrollBy(dx, dy);
                mChsvContent.scrollBy(dx, dy);

                mHlvContentG.addOnScrollListener(scrollListeners[1]);
            }
        };
        scrollListeners[1] = new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                totalDx += dx;
                mTopLayout.setVisibility(totalDx == 0 ? View.GONE : View.VISIBLE);
                mRvLine.removeOnScrollListener(scrollListeners[0]);

                mRvLine.scrollBy(dx, dy);
                mChsvContent.scrollBy(dx, dy);

                mRvLine.addOnScrollListener(scrollListeners[0]);
            }
        };

        mRvLine.addOnScrollListener(scrollListeners[0]);
        mHlvContentG.addOnScrollListener(scrollListeners[1]);
    }

    private int totalDx = 0;
}
